package logic

import (
	"context"
	"microdemo/service/user/internal/svc"
	"microdemo/service/user/user"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserLogic) GetUser(in *user.IdRequest) (*user.UserResponse, error) {
	atoi, err := strconv.Atoi(in.Id)
	if err != nil {
		return nil, err
	}
	one, err := l.svcCtx.UserModel.FindOne(l.ctx, int64(atoi))
	// 注意：如果没有查询到数据，会报 error "no rows in result set"
	if err != nil {
		return nil, err
	}
	return &user.UserResponse{
		Id:     strconv.FormatInt(one.Id, 10),
		Name:   one.Name,
		Gender: one.Gender,
	}, nil
}
